Amarkal.settings.sections.initSections   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
1
Amarkal.settings.sections = {
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
2
    $links: null,
3
    $loader: null,
4
    data: null,
5
    activeSection: null,
6
    init: function() {
7
        this.data = JSON.parse($('#sections-config').text());
8
        this.$links = $('.amarkal-settings-sections li');
9
        this.$loader = $('#amarkal-settings-loader');
10
        this.$form = $('#amarkal-settings-form');
11
        this.$fields = this.$form.find('.amarkal-settings-field');
12
13
        this.initSections();
14
    },
15
    initSections: function() {
16
        if(typeof this.data === 'object' && Object.keys(this.data).length > 0) {
17
            var _this = this;
18
            this.$links.on('click', function(){
19
                _this.activate($(this).attr('data-slug'));
20
            });
21
            this.activateInitialSection();
22
            this.$loader.hide();
23
        }
24
        else {
25
            Amarkal.settings.fields.showAll();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
26
        }
27
    },
28
    activateInitialSection: function() {
29
        if('' !== window.location.hash) {
30
            this.activate(window.location.hash.substring(1));
31
        }
32
        else {
33
            this.activate($(this.$links[0]).attr('data-slug'));
34
        }
35
    },
36
    activate: function(sectionSlug) {
37
        if(this.activeSection === sectionSlug) {
38
            return;
39
        }
40
        this.activeSection = sectionSlug;
41
42
        this.$links
43
            .removeClass('active')
44
            .filter('[data-slug="'+sectionSlug+'"]')
45
            .addClass('active');
46
47
        Amarkal.settings.header.setSectionTitle(this.getTitle(sectionSlug));
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
48
        Amarkal.settings.header.setSectionSubtitle(this.getSubtitle(sectionSlug));
49
        Amarkal.settings.fields.hideAll();
50
        Amarkal.settings.fields.show(this.getSectionFields(sectionSlug));
51
52
        window.location = '#'+sectionSlug;
53
    },
54
    deactivate: function() {
55
        var sectionSlug = this.activeSection;
0 ignored issues
show
Unused Code introduced by
The variable sectionSlug seems to be never used. Consider removing it.
Loading history...
56
        this.$links.removeClass('active');
57
        Amarkal.settings.fields.hideAll();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
58
        this.activeSection = null;
59
    },
60
    flag: function(type, slug) {
61
        this.$links.filter('[data-slug="'+slug+'"]').addClass('flag-'+type);
62
    },
63
    unflag: function(type, slug) {
64
        this.$links.filter('[data-slug="'+slug+'"]').removeClass('flag-'+type);
65
    },
66
    unflagAll: function() {
67
        this.$links.removeClass('flag-error flag-notice');
68
    },
69
    getTitle: function(slug) {
70
        return this.data[slug].title;
71
    },
72
    getSubtitle: function(slug) {
73
        return this.data[slug].subtitle;
74
    },
75
    getSectionFields: function(slug) {
76
        return this.$fields.filter('[data-section="'+slug+'"]');
77
    },
78
    changed: function(slug) {
79
        var changed = false;
80
        this.getSectionFields(slug).each(function(){
81
            if($(this).find('.amarkal-ui-component').amarkalUIComponent('changed')) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if $(this).find(".amarkal-u...lUIComponent("changed") is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
82
                changed = true;
83
                return false;
84
            }
85
        });
86
        return changed;
87
    }
88
};